package com.qire.common.receiver;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.TelephonyManager;

import com.qire.common.SummerApp.AppActiveRegistrar;
import com.qire.common.utils.Logger;

import java.util.ArrayList;

/**
 * Sim 状态改变，拔插SIM等监听
 */
public class SimChangeReceiver  extends BroadcastReceiver implements AppActiveRegistrar {

    private final static String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";

    private final ArrayList<AbsentSimAction> absentList = new ArrayList<>();
    private final ArrayList<ReadySimAction> readyList = new ArrayList<>();

    private boolean isRegistered = false;
    private boolean isSimValid = false;

    public void registerReceiver(Context context) {
        if(isRegistered) {
            return;
        }
        isRegistered = true;
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_SIM_STATE_CHANGED);
        context.registerReceiver(this, filter);
        return;
    }

    public void unregisterReceiver(Context context) {
        if(isRegistered) {
            isRegistered = false;
            context.unregisterReceiver(this);
        }
        absentList.clear();
        readyList.clear();
    }

    public SimChangeReceiver addAbsentSimAction(AbsentSimAction absentSimAction) {
        for(AbsentSimAction action : absentList) {
            if(action.id() == absentSimAction.id()) {
                return this;
            }
        }
        absentList.add(absentSimAction);
        return this;
    }
    public SimChangeReceiver addReadySimAction(ReadySimAction readySimAction) {
        for(ReadySimAction action : readyList) {
            if(action.id() == readySimAction.id()) {
                return this;
            }
        }
        readyList.add(readySimAction);
        return this;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        switch (action) {
            case ACTION_SIM_STATE_CHANGED : {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
                onSimStateChange(tm.getSimState());
                return;
            }
        }
    }

    private void onSimStateChange(int simState) {
        switch (simState) {
            case TelephonyManager.SIM_STATE_READY:
                // SIM 已激活，插入状态的
                Logger.d(String.format("SIM卡状态切换为：已激活，状态代码：%s", simState));
                isSimValid = true;
                for(ReadySimAction action : readyList) {
                    action.onReady();
                }
                break;
            case TelephonyManager.SIM_STATE_UNKNOWN:
            case TelephonyManager.SIM_STATE_ABSENT:
                // SIM 未激活，缺少的，拔出状态的
            case TelephonyManager.SIM_STATE_PIN_REQUIRED:
            case TelephonyManager.SIM_STATE_PUK_REQUIRED:
            case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
            default:
                Logger.d(String.format("SIM卡状态切换为：已失效，状态代码：%s", simState));
                isSimValid = false;
                for(AbsentSimAction action : absentList) {
                    action.onAbsent();
                }
                break;
        }
    }

    public interface AbsentSimAction {
        default int id() {
            return 0;
        }
        void onAbsent();
    }

    public interface ReadySimAction {
        default int id() {
            return 0;
        }
        void onReady();
    }

}
